﻿Imports System.Linq
Imports System
Imports System.Collections.Generic
Imports System.Collections.ObjectModel
Imports System.Collections.Specialized
Imports System.ComponentModel
Imports System.Diagnostics
Imports System.Windows.Data
Imports PTIC_GestionBDD
Imports PTIC_Util
Imports OMANE.Common.UI
Imports System.Windows.Input
Imports Microsoft.Practices.Prism.Commands

Namespace ViewModels
    ''' <summary>
    ''' View Model de l'écran de paramétrage des correspondances du fichier d'encours
    ''' </summary>
    ''' <remarks></remarks>
    Public Class ViewModelCmdEncoursCorresp
        Inherits ViewModelCommand
        Implements IViewModelTabSection

#Region "Variables privées"
        Private _model As ModelTauxGarantis
#End Region

#Region "Constructeur"
        ''' <summary>
        ''' Constructeur
        ''' </summary>
        ''' <remarks></remarks>
        Public Sub New(ByVal Model As ModelTauxGarantis, ByVal DisplayName As String, ByVal Command As ICommand)
            MyBase.New(DisplayName, Command)
            _model = Model
            Dim traitTbleEncours As tableBDD = _model.ListeTraitements.TraitTbleEncours
            'Ajouter une capture de l'évènement indiquant que le nom de la table a changé
        End Sub
        Public Sub New()
        End Sub
#End Region

#Region "Propriétés"

        Public Property IsClosable As Boolean Implements IViewModelTabSection.IsClosable
        Public Property Icone As String Implements Common.UI.IViewModelTabSection.Icone
        Public Property Title As String Implements Common.UI.IViewModelTabSection.Title

        ''' <summary>
        ''' Liste des champs attendus pour l'import des données d'encours
        ''' </summary>
        ''' <remarks></remarks>
        Public ReadOnly Property VariablesEntreeEncours() As ObservableDictionary(Of String, Correspondance)
            Get
                Return _model.ListeTraitements.TraitTbleEncours.ListeChampsSortie
            End Get
        End Property

        ''' <summary>
        ''' Liste des champs disponibles dans la table sélectionnée par l'utilisateur
        ''' </summary>
        ''' <remarks></remarks>
        Public ReadOnly Property VariablesEntreeBase() As Dictionary(Of String, Correspondance)
            Get
                Return _model.ListeTraitements.TraitTbleEncours.ListeChampsEntree
            End Get
        End Property

        Private _cmdVisualiserChampsEntree As ICommand
        Public ReadOnly Property cmdVisualiserChampsEntree As ICommand
            Get
                If _cmdVisualiserChampsEntree Is Nothing Then
                    _cmdVisualiserChampsEntree = New DelegateCommand(Of Object)(AddressOf VisualiserChampsEntree)
                End If
                Return _cmdVisualiserChampsEntree
            End Get
        End Property

        Private _cmdChargerFichierCorresp As ICommand
        Public ReadOnly Property cmdChargerFichierCorresp() As ICommand
            Get
                If _cmdChargerFichierCorresp Is Nothing Then
                    _cmdChargerFichierCorresp = New DelegateCommand(AddressOf ChargerFichierCorrespondances)
                End If
                Return _cmdChargerFichierCorresp
            End Get
        End Property

        Private _cmdEnregistrerFichierCorresp As ICommand
        Public ReadOnly Property cmdEnregistrerFichierCorresp() As ICommand
            Get
                If _cmdEnregistrerFichierCorresp Is Nothing Then
                    _cmdEnregistrerFichierCorresp = New DelegateCommand(AddressOf EnregistrerFichierCorrespondances)
                End If
                Return _cmdEnregistrerFichierCorresp
            End Get
        End Property

        Private _cmdEditerCorrespondance As ICommand
        Public ReadOnly Property cmdEditerCorrespondance As ICommand
            Get
                If _cmdEditerCorrespondance Is Nothing Then
                    _cmdEditerCorrespondance = New DelegateCommand(Of Object)(AddressOf EditerCorrespondance)
                End If
                Return _cmdEditerCorrespondance
            End Get
        End Property

        Private _cmdSupprimerCorrespondance As ICommand
        Public ReadOnly Property cmdSupprimerCorrespondance As ICommand
            Get
                If _cmdSupprimerCorrespondance Is Nothing Then
                    _cmdSupprimerCorrespondance = New DelegateCommand(Of Object)(AddressOf SupprimerCorrespondance)
                End If
                Return _cmdSupprimerCorrespondance
            End Get
        End Property

#End Region

#Region "Commandes"
        ''' <summary>
        ''' Méthode appelée lorsqu'un champs attendu en entrée de la table d'encours est renseigné par glisser déposer
        ''' </summary>
        ''' <remarks></remarks>
        Public Sub paramChampsCible(ByVal sender As Object, ByVal e As System.Windows.DragEventArgs)
            Dim chCible As Correspondance = CType(sender.DataContext, KeyValuePair(Of String, Correspondance)).Value
            Dim chSource As Correspondance = e.Data.GetData(GetType(KeyValuePair(Of String, Correspondance))).Value
            Dim tableEncours As tableBDD = _model.ListeTraitements.TraitTbleEncours
            Dim result As Boolean = _model.ListeTraitements.TraitTbleEncours.setChSortieFormuleEgaleChampsEntree(chSource.description, chCible.description)
            '_model.Univers.listeTraitements.getTraitTbleEncours.ListeChampsSortie.setDescription(chCible.description, "totot")
            'chCible.typeChEntree = typeChEntreeMapping.CONSTANT
            'VariablesEntreeEncours.NotifyCollectionChange(chCible.description, chCible)
        End Sub

        ''' <summary>
        ''' Procédure de chargement d'un mapping des champs de la table d'encours
        ''' </summary>
        ''' <remarks></remarks>
        Private Sub ChargerFichierCorrespondances()
            Dim openFileDialog1 As New Microsoft.Win32.OpenFileDialog
            openFileDialog1.InitialDirectory = csteEmplacementParametrage
            openFileDialog1.Filter = "Corresp. encours (*.oma)|*.oma"
            If openFileDialog1.ShowDialog() Then
                'Chargement des correspondances sauvegardées
                baseListeChampsSortieTable.load(openFileDialog1.FileName, _model.ListeTraitements.TraitTbleEncours)
            End If
        End Sub

        ''' <summary>
        ''' Enregistrer le mapping en cours de la table d'encours
        ''' </summary>
        ''' <remarks></remarks>
        Private Sub EnregistrerFichierCorrespondances()
            Dim SaveFileDialog1 As New Microsoft.Win32.SaveFileDialog
            SaveFileDialog1.InitialDirectory = csteEmplacementParametrage
            SaveFileDialog1.Filter = "Fichiers de correspondance encours (*.oma)|*.oma"
            If SaveFileDialog1.ShowDialog() Then
                'Sauvegarde des objets métiers
                Dim mapTxGar As baseListeChampsSortieTable = _model.ListeTraitements.TraitTbleEncours.ListeChampsSortie
                mapTxGar.save(SaveFileDialog1.FileName)
                MsgBox("Le mapping courant de la table d'encours a été enregistré sous " & SaveFileDialog1.FileName)
            End If
        End Sub

        ''' <summary>
        ''' Visualiser les caractéristiques d'un champs de la base retenue en entrée pour les données d'encours
        ''' </summary>
        ''' <param name="correspondance"></param>
        ''' <remarks></remarks>
        Private Sub VisualiserChampsEntree(ByVal correspondance As Object)
            Dim correspAVisualiser As Correspondance = correspondance
            Dim VMVariable As New ViewModelVariable(correspAVisualiser)
            Dim ui = ModuleTauxGaranti._app.GetService(Of IUIService)()
            ui.ShowMessage(VMVariable)
        End Sub

        Private Sub SupprimerCorrespondance(ByVal CorrespondanceASupprimer As Object)
            Dim correspASupprimer As Correspondance = CorrespondanceASupprimer
            correspASupprimer.setChEntree(Nothing)
        End Sub

        Private Sub EditerCorrespondance(ByVal Correspondance As Object)
            Dim correspAEditer As Correspondance = Correspondance
            Dim VMCorresp As New ViewModelCorrespondance(correspAEditer)
            Dim ui = ModuleTauxGaranti._app.GetService(Of IUIService)()
            ui.ShowMessage(VMCorresp)
        End Sub

#End Region

    End Class
End Namespace
